package com.pet.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pet.article.feign.*;
import com.pet.article.mapper.ApArticleConfigMapper;
import com.pet.article.mapper.ApArticleContentMapper;
import com.pet.article.mapper.ApAuthorMapper;
import com.pet.article.service.ApCollectionService;
import com.pet.article.service.ArticleInfoService;
import com.pet.article.feign.ApBehaviorEntryFeign;
import com.pet.article.feign.ApLikesBehaviorFeign;
import com.pet.article.feign.ApUnlikesBehaviorFeign;
import com.pet.article.feign.ApUserFollowFeign;
import com.pet.common.exception.LeadNewsException;
import com.pet.model.article.dtos.ArticleBehaviorDto;
import com.pet.model.article.dtos.ArticleInfoDto;
import com.pet.model.article.pojos.ApArticleConfig;
import com.pet.model.article.pojos.ApArticleContent;
import com.pet.model.article.pojos.ApAuthor;
import com.pet.model.article.pojos.ApCollection;
import com.pet.model.behavior.pojos.ApBehaviorEntry;
import com.pet.model.behavior.pojos.ApLikesBehavior;
import com.pet.model.behavior.pojos.ApUnlikesBehavior;
import com.pet.model.common.dtos.ResponseResult;
import com.pet.model.common.enums.AppHttpCodeEnum;
import com.pet.model.user.pojos.ApUser;
import com.pet.model.user.pojos.ApUserFollow;
import com.pet.utils.threadlocal.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ArticleInfoServiceImpl implements ArticleInfoService {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApBehaviorEntryFeign apBehaviorEntryFeign;
    @Autowired
    private ApCollectionService apCollectionService;
    @Autowired
    private ApUnlikesBehaviorFeign apUnlikesBehaviorFeign;
    @Autowired
    private ApLikesBehaviorFeign apLikesBehaviorFeign;
    @Autowired
    private ApUserFollowFeign apUserFollowFeign;
    @Autowired
    private ApAuthorMapper apAuthorMapper;

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {

        Map<String, Object> resultMap = new HashMap<>();

        //1.检查参数
        if(dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.查询文章的配置
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if(apArticleConfig == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //3.查询文章的内容
        if(apArticleConfig.getIsDelete() != 1&& apArticleConfig.getIsDown() != 0){
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
            resultMap.put("content",apArticleContent);
        }
        resultMap.put("config",apArticleConfig);
        //4.结果返回
        return ResponseResult.okResult(resultMap);
    }

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDto dto) {
        //1.查询登录用户
        ApUser apUser = (ApUser) ThreadLocalUtils.get();
        Integer userId=null;
        if(apUser!=null){
            userId=apUser.getId();
        }
        //2.获取设备ID
        Integer equipmentId = dto.getEquipmentId();


        //3.获取行为实体
        ApBehaviorEntry behaviorEntry = apBehaviorEntryFeign.findByUserIdOrEquipmentId(userId, equipmentId);
        if(behaviorEntry==null){
            throw new LeadNewsException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        try {
            Boolean isFollow=false,isLike=false,isUnLike=false,isCollection=false;
            //4.查询收藏行为数据
            ApCollection apCollection = apCollectionService.getApCollection(behaviorEntry.getId(), dto.getArticleId());
            if(apCollection!=null){
                isCollection=true;
            }

            //5.查询点赞行为数据
            ApLikesBehavior likesBehavior = apLikesBehaviorFeign.getLikeBehavior(behaviorEntry.getId(), dto.getArticleId()).getData();
            if(likesBehavior!=null&&likesBehavior.getType().equals(0)){
                isLike=true;
            }

            //6.查询不喜欢行为数据
            ApUnlikesBehavior unlikesBehavior = apUnlikesBehaviorFeign.getUnlikeBehavior(behaviorEntry.getId(), dto.getArticleId()).getData();
            if(unlikesBehavior!=null&&unlikesBehavior.getType().equals(0)){
                isUnLike=true;
            }

            //7.查询用户关注作者数据
            if(apUser!=null){
                //1.根据作者ID查询作者，获取userId
                ApAuthor apAuthor = apAuthorMapper.selectById(dto.getAuthorId());
                ApUserFollow userFollow = apUserFollowFeign.getUserFollow(userId, apAuthor.getUserId()).getData();
                if(userFollow!=null){
                    isFollow=true;
                }
            }

            //8.封装Map集合，并返回
            Map<String, Object> map = new HashMap<>();
            map.put("isfollow",isFollow);
            map.put("islike",isLike);
            map.put("isunlike",isUnLike);
            map.put("iscollection",isCollection);
            return ResponseResult.okResult(map);
        } catch (LeadNewsException e) {
            e.printStackTrace();
            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
}